<?php
declare (strict_types=1);

namespace app\v1\controller;

use app\data\logic\PageLogic;
use app\v1\BaseController;
use think\App;
use think\Request;

class CmsSites extends BaseController
{
    protected PageLogic $pageLogic;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->pageLogic = new PageLogic();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $result = $this->pageLogic->getCmsSitesList($this->request->get());
        if ($result) {
            return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'), $result['list'], $result['count']);
        } else {
            return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $param = $request->param();
        $result = $this->pageLogic->createCmsSites($param);
        if ($result['state']) {
            return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
        } else {
            return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        $result = $this->pageLogic->getCmsSitesDetail($id);
        if ($result['state']) {
            return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'), $result['data']);
        } else {
            return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = $request->put();
        $checkData['id'] = $id;
        $result = $this->pageLogic->updateCmsSites($id, $params, $checkData);
        if ($result['state']) {
            return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
        } else {
            return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = $this->pageLogic->deleteCmsSites($id);
        if ($result['state']) {
            return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
        } else {
            return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
        }
    }
}
